package cn.hahah.module.blog.controller.admin.usercomment;

import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.usercomment.vo.*;
import cn.hahah.module.blog.dal.dataobject.usercomment.UserCommentDO;
import cn.hahah.module.blog.service.usercomment.UserCommentService;

@Tag(name = "管理后台 - 用户评论文章")
@RestController
@RequestMapping("/blog/user-comment")
@Validated
public class UserCommentController {

    @Resource
    private UserCommentService userCommentService;
    @Resource
    private UsersService usersService;
    @Resource
    private ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建用户评论文章")
    @PreAuthorize("@ss.hasPermission('blog:user-comment:create')")
    public CommonResult<Long> createUserComment(@Valid @RequestBody UserCommentSaveReqVO createReqVO) {
        return success(userCommentService.createUserComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户评论文章")
    @PreAuthorize("@ss.hasPermission('blog:user-comment:update')")
    public CommonResult<Boolean> updateUserComment(@Valid @RequestBody UserCommentSaveReqVO updateReqVO) {
        userCommentService.updateUserComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户评论文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-comment:delete')")
    public CommonResult<Boolean> deleteUserComment(@RequestParam("id") Long id) {
        userCommentService.deleteUserComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户评论文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-comment:query')")
    public CommonResult<UserCommentRespVO> getUserComment(@RequestParam("id") Long id) {
        UserCommentDO userComment = userCommentService.getUserComment(id);
        return success(BeanUtils.toBean(userComment, UserCommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户评论文章分页")
    @PreAuthorize("@ss.hasPermission('blog:user-comment:query')")
    public CommonResult<PageResult<UserCommentRespVO>> getUserCommentPage(@Valid UserCommentPageReqVO pageReqVO) {
        PageResult<UserCommentDO> pageResult = userCommentService.getUserCommentPage(pageReqVO);
        PageResult<UserCommentRespVO> result = BeanUtils.toBean(pageResult, UserCommentRespVO.class);
        for (UserCommentRespVO userCommentRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userCommentRespVO.getUserId());
            ArticleDO articleDO = articleService.getArticle(userCommentRespVO.getArticleId());

            if( usersDO != null ){
                userCommentRespVO.setUserName(usersDO.getUsername());
            }
            if( articleDO != null ){
                userCommentRespVO.setArticleName(articleDO.getTitle());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户评论文章 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-comment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCommentExcel(@Valid UserCommentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserCommentDO> list = userCommentService.getUserCommentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户评论文章.xls", "数据", UserCommentRespVO.class,
                        BeanUtils.toBean(list, UserCommentRespVO.class));
    }

}